<template>
  <div class="approval">
    <el-card class="box-card" style="margin-top: 110px;">
      <div class="head-title2">
        <div class='title'>
          {{$t('achievements.title1')}}
        </div>
        <div class="info">
          <el-tooltip placement="left">
            <div slot="content">
              {{define}}
            </div>
            <div class="btns"></div>
          </el-tooltip>
        </div>
      </div>
      <div class="menu-search">
        <div class="menu-title">
            <!-- 选择时间 -->
            <div class="searchItem searchItem-date">
                <span>{{$t('ApprReport.choiceTime')}}</span>
                <el-date-picker
                size="small"
                v-model="choiceDate"
                type="daterange"
                :range-separator="$t('btn.go')"
                :start-placeholder="$t('rule.beginDate')"
                :end-placeholder="$t('rule.endDate')"
                value-format="yyyy-MM-dd"
                @change="dateChangeFirstCompleteDate"
                ></el-date-picker>
            </div>
            <!-- APP渠道 -->
            <div class="searchItem">
              <span>{{$t('ElectricChannel.AppChannel')}}</span>
              <el-select v-model="listQuery.channel" size="small">
                <el-option
                  v-for="item in appChannel"
                  :key="item.channel"
                  :label="item.org_name"
                  :value="item.channel"
                ></el-option>
              </el-select>
            </div>
            <div class="menu-title-btn">
              <button type="button" @click="search">{{$t('btn.query')}}</button>
            </div>
          </div>
      </div>
    </el-card>
    <el-card class="box-card" style="margin-top: 10px;margin-bottom: 20px;">
      <div class="title">{{$t('DailyStatistics.ListDetails')}}</div>
      <el-table
        v-loading="listLoading"
        ref="multipleTable"
        :data="tableData"
        tooltip-effect="dark"
        style="width: 100%"
        border
        stripe
        size="mini"
      >
        <!-- 时间 -->
        <el-table-column header-align="center" align="center" prop="date" :label="$t('approvalDetail.riskTime')"></el-table-column>
        <!-- 渠道 -->
        <el-table-column header-align="center" align="center" :label="$t('productManage.channels')">
          <template>
            <span>{{currentChannel}}</span>
          </template>
        </el-table-column>
        <!-- 机审拒绝率 -->
        <el-table-column header-align="center" align="center" prop="machineRefuseRate" :label="$t('ProcessRefuseRate.AirRejectrate')"></el-table-column>
        <!-- 初审拒绝率 -->
        <el-table-column header-align="center" align="center" prop="firstRefuseRate" :label="$t('ProcessRefuseRate.FirstRejectrate')"></el-table-column>
        <!-- 复审拒绝率 -->
        <el-table-column header-align="center" align="center" prop="complexRefuseRate" :label="$t('ProcessRefuseRate.ReviewRejectrate')"></el-table-column>
        <!-- 终审拒绝率 -->
        <el-table-column header-align="center" align="center" prop="finallRefuseRate" :label="$t('ProcessRefuseRate.EndRejectrate')"></el-table-column>
        <!-- 放款率 -->
        <el-table-column header-align="center" align="center" prop="cashoutRate" :label="$t('ProcessRefuseRate.LoanRate')"></el-table-column>
      </el-table>
      <div class="pagination-container">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="listQuery.page"
          :page-sizes="[5, 10, 20, 30]"
          :page-size="listQuery.limit"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        ></el-pagination>
      </div>
    </el-card>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import { toPercent } from "utils/index";
import { getChannels } from "api/customQuery/index";
import { flowRefuse } from "api/reportForm/OperationReport/ProcessRefuseRate/index";
export default {
  data() {
    return {
      define: '',
      choiceDate: [new Date(new Date().getFullYear(), new Date().getMonth(), new Date().getDate() - 6), new Date()],
      listLoading: false,
      total: null,
      listQuery: {
        limit: 5,
        page: 1,
        startDate: '', 
        endDate: '',
        channel: ''
      },
      tableData: [],
      appChannel: [],
      currentChannel: ''
    };
  },
  created() {
    if (this.RefuseRatePage.has(this.$route.path)) {
      this.listQuery = this.RefuseRatePage.get(this.$route.path);
    }
    this.getTime();
    // 渠道下拉
    getChannels().then(res => {
      this.appChannel = res.data;
      this.listQuery.channel = this.appChannel[0].channel;
      this.currentChannel = this.appChannel[0].org_name;
      this.getList();
    });
    // 获取报表定义
    this.getReportDefine.forEach(item => {
      if (item.dictCode === 'PROCESS_REJECTION_RATE') {
        this.define = item.dictValue
      }
    })
  },
  computed: {
    ...mapGetters(["elements", "RefuseRatePage", 'getReportDefine'])
  },
  methods: {
    getTime() {
      this.listQuery.startDate = this.GetDateStr(-6);
      this.listQuery.endDate = this.GetDateStr(0);
    },
    GetDateStr(AddDayCount) {
      let dd = new Date();
      dd.setDate(dd.getDate() + AddDayCount);// 获取AddDayCount天后的日期
      let y = dd.getFullYear();
      let m = (dd.getMonth() + 1) < 10 ? '0' + (dd.getMonth() + 1) : (dd.getMonth() + 1); // 获取当前月份的日期，不足10补0
      let d = dd.getDate() < 10 ? '0' + dd.getDate() : dd.getDate(); // 获取当前几号，不足10补0
      return y + '-' + m + '-' + d;
    },
    // 选择时间
    dateChangeFirstCompleteDate(val) {
      if (val === undefined || val === null || val === '') {
        this.listQuery.startDate = '';
        this.listQuery.endDate = '';
      } else {
        this.listQuery.startDate = val[0];
        this.listQuery.endDate = val[1];
      }
    },
    getList() {
      if (this.listQuery.startDate === '' || this.listQuery.startDate === null || this.listQuery.startDate === undefined) {
        this.$message({
          showClose: true,
          type: "warning",
          message: this.$t('CollStatisReport.ChooseDate')
        });
      } else if (this.listQuery.endDate === '' || this.listQuery.endDate === null || this.listQuery.endDate === undefined) {
        this.$message({
          showClose: true,
          type: "warning",
          message: this.$t('CollStatisReport.ChooseDate')
        });
      } else {
        this.listLoading = true;
        this.$store.dispatch("saveRefuseRatePage", {
          path: this.$route.path,
          listQuery: this.listQuery
        });
        flowRefuse(this.listQuery).then(res => {
          if (res.status === 200) {
            this.listLoading = false;
            this.appChannel.forEach(item => {
              if (this.listQuery.channel === item.channel) {
                this.currentChannel = item.org_name;
              }
            })
            res.data.flowRefuseList.forEach(item => {
              if (item.machineRefuseRate ||
              item.firstRefuseRate ||
              item.complexRefuseRate ||
              item.finallRefuseRate ||
              item.cashoutRate) {
                item.machineRefuseRate = toPercent(item.machineRefuseRate);
                item.firstRefuseRate = toPercent(item.firstRefuseRate);
                item.complexRefuseRate = toPercent(item.complexRefuseRate);
                item.finallRefuseRate = toPercent(item.finallRefuseRate);
                item.cashoutRate = toPercent(item.cashoutRate);
              }
            });
            this.tableData = res.data.flowRefuseList;
            this.total = Number(res.data.total);
          } else {
            this.$message({
              showClose: true,
              message: res.message,
              type: "warning"
            });
          }
        });
      }
    },
    //查询
    search() {
      this.listQuery.page = 1;
      this.getList();
    },
    handleSizeChange(val) {
      this.listQuery.limit = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.listQuery.page = val;
      this.getList();
    }
  }
};
</script>

<style scoped lang='scss'>
.box-card {
  width: 95%;
  margin-left: 2.5%;
  margin-right: 2.5%;
}
.title {
  line-height: 30px;
  font-size: 16px;
  display: flex;
  align-items: center;
  font-weight: bold;
}
.title:before {
  display: block;
  content: "";
  width: 3px;
  height: 16px;
  background-color: #309afe;
}
.title:before {
  margin-right: 8px;
}
::before {
  box-sizing: inherit;
  -webkit-tap-highlight-color: transparent;
}
.menu-title {
  width: 100%;
  box-sizing: border-box;
  background: #ffffff;
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  .searchItem {
    display: flex;
    align-items: center;
    padding: 10px 0;
    span {
      display: inline-block;
      width: 100px;
      text-align: right;
      font-size: 12px;
      color: #5f5f61;
      padding-right: 5px;
    }
    div:nth-child(2) {
      width: 150px;
    }
  }
  .searchItem-date {
    display: flex;
    align-items: center;
    padding: 10px 0;
    span {
      display: inline-block;
      width: 100px;
      text-align: right;
      font-size: 12px;
      color: #5f5f61;
      padding-right: 5px;
    }
    div:nth-child(2) {
      width: 220px;
    }
  }
}
.menu-title-btn {
  padding: 10px 0;
  box-sizing: border-box;
  margin-left: 20px;
  button {
    padding: 0 20px;
    height: 28px;
    background: #20a0ff;
    color: #fff;
    outline: none;
    border-width: 0;
    border-radius: 4px;
    font-size: 12px;
    margin-left: 10px;
    &:nth-child(1) {
      margin-left: 0;
    }
    &:nth-child(2) {
      background: #ebf5ff;
      color: #20a0ff;
      border: 1px solid #abd5ff;
    }
  }
  button[disabled] {
    padding: 0 20px;
    height: 28px;
    background: #c1e0ff;
    color: #fafbff;
    outline: none;
    border-width: 0;
    border-radius: 4px;
    font-size: 12px;
  }
}
.menu-list {
  padding: 20px 0;
  box-sizing: border-box;
  button {
    padding: 0 20px;
    height: 28px;
    background: #20a0ff;
    color: #fff;
    outline: none;
    border-width: 0;
    border-radius: 4px;
    font-size: 12px;
    margin-left: 10px;
    &:nth-child(1) {
      margin-left: 0;
    }
  }
  button[disabled] {
    padding: 0 20px;
    height: 28px;
    background: #c1e0ff;
    color: #fafbff;
    outline: none;
    border-width: 0;
    border-radius: 4px;
    font-size: 12px;
  }
}
.href {
  border-bottom: 1px solid #000;
  cursor: pointer;
  &:hover {
    color: #20a0ff;
    border-bottom: 1px solid #20a0ff;
  }
}
.head-title2 {
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.btns {
  display: inline-block;
  width: 24px;
  height: 24px;
  background-size: cover;
  background-image: url("../../../../assets/images/question-mark.svg");
}
</style>